<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="header-title">
        <img src="../assets/heima.png" alt="">
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu :default-active="menuActivePath" class="el-menu-vertical-demo" background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened :collapse="isCollapse" :collapse-transition="false" router>
          <!-- 一级菜单 -->
          <el-submenu :index="items.id+''" v-for="items in menuList" :key="items.id">
            <!-- 一级模板区域 -->
            <template slot="title">
              <i :class="iconObj[items.id]"></i>
              <span>{{ items.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+item.path" v-for="item in items.children" :key="item.id" @click="savePath('/'+item.path)">
              <!-- 二级模板区域 -->
              <i class="el-icon-location"></i>
              <span>{{ item.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <!-- 右侧内容主题 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import request from '../network/request'

export default {
  name: 'MyHome',
  data() {
    return {
      // 菜单列表
      menuList: [],
      // icon列表
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 控制左边菜单栏的状态
      isCollapse: false,
      menuActivePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.menuActivePath = window.sessionStorage.getItem('menuActivePath')
  },
  methods: {
    // 退出登录
    logout() {
      // 清除登录Token
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单列表
    async getMenuList() {
      const { data: res } = await request.get('/menus')
      if (res.meta.status !== 200) return this.meta.msg
      this.menuList = res.data
    },
    // 定义左边菜单状态
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存菜单栏点击后的路径
    savePath(menuActivePath) {
      this.menuActivePath = menuActivePath
      window.sessionStorage.setItem('menuActivePath', menuActivePath)
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  user-select: none;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
  padding-left: 0;
  .header-title {
    display: flex;
    align-items: center;
    color: #fff;
    font-size: 20px;
    span {
      margin-left: 14px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
  .toggle-button {
    background-color: #4a5064;
    line-height: 24px;
    text-align: center;
    color: #fff;
    font-size: 10px;
    letter-spacing: 0.2em;
    cursor: pointer;
    user-select: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
